<?php

class Track {

    private $_position;
    private $_title;
    private $_duration;
    private $_artists = array();
    private $_extraartists = array();

    public function getPosition() {
        return $this->_position;
    }

    public function setPosition($position) {
        $this->_position = $position;
        return $this;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($title) {
        $this->_title = $title;
        return $this;
    }

    public function getDuration() {
        return $this->_duration;
    }

    public function setDuration($duration) {
        $this->_duration = $duration;
        return $this;
    }

    public function getArtists() {
        return $this->_artists;
    }

    public function setArtists($artists) {
        $this->_artists = $artists;
        return $this;
    }

    public function getExtraartists() {
        return $this->_extraartists;
    }

    public function setExtraartists($extraartists) {
        $this->_extraartists = $extraartists;
        return $this;
    }

    function __construct(array $donnees) {
        foreach ($donnees as $key => $value) {
            if (($key == 'artists') || ($key == 'extraartists')) {
                $donnees[$key] = Factory::fillArrayWithObjects($donnees[$key],'artist');
            } else {
                if (isset($donnees[$key][0]['__content__'])) {
                    $donnees[$key] = $donnees[$key][0]['__content__'];
                } else {
                    $donnees[$key] = $donnees[$key][0];
                }
            }
        }

        $this->hydrate($donnees);
    }
    
    public function __toArray(){
        return array('position' => $this->_position,
            'title' => $this->_title,
            'duration' => $this->_duration,
            'artists' => $this->_artists,
            'extraartists' => $this->_extraartists);
    }
    
    public function __toString() {
       return print_r(array('position' => $this->_position,
            'title' => $this->_title,
            'duration' => $this->_duration,
            'artists' => $this->_artists->__toString(),
            'extraartists' => $this->_extraartists->__toString()),true);
        
        //return print_r($this->__toArray(), true);
    }
    
    public function hydrate(array $donnees) {
        foreach ($donnees as $key => $value) {
// On récupère le nom du setter correspondant à l'attribut.
            $method = 'set' . ucfirst($key);

// Si le setter correspondant existe.
            if (method_exists($this, $method)) {
// On appelle le setter.
                $this->$method($value);
            }
        }
    }

}

?>
